package com.topica;

import java.text.ParseException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.topica.adapter.AdapterCourse;
import com.topica.database.callback.OnGetListCourseListener;
import com.topica.database.callback.OnGetListWeekListener;
import com.topica.database.task.GetListCourseTask;
import com.topica.database.task.GetListWeekTask;
import com.topica.network.SendMultiMark;
import com.topica.network.callback.OnSendMultiMarkListener;
import com.topica.object.CourseItem;
import com.topica.object.WeekItem;
import com.topica.util.Util;

public class TopicaCourse extends Activity implements OnItemClickListener,
		OnGetListCourseListener, OnGetListWeekListener, OnSendMultiMarkListener {
	private ListView mListCourse;
	private TextView txtGreeting;
	private AdapterCourse mAdapterCourse;
	private boolean isFirstTime;
	private String courseName = "";
	private SharedPreferences prefCourse;
	public static String PREF_COURSE = "_course";
	public static String COURSE_NAME_KEY = "_course_key";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_topica_course);
		prefCourse = getSharedPreferences(PREF_COURSE, 0);
		mListCourse = (ListView) findViewById(R.id.listCourse);
		txtGreeting = (TextView) findViewById(R.id.txt_greeting);
		mAdapterCourse = new AdapterCourse(getApplicationContext(), -1,
				new ArrayList<CourseItem>());
		mListCourse.setAdapter(mAdapterCourse);
		mListCourse.setOnItemClickListener(this);
		isFirstTime = true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isFirstTime) {
			isFirstTime = false;
			GetListCourseTask task = new GetListCourseTask(this);
			task.setOnGetListCourseListener(this);
			task.execute();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_topica_course, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_update:
			Intent intent = new Intent(getBaseContext(), TopicaUpdate.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.menu_home:

			break;
		case R.id.menu_calendar:

			break;
		case R.id.menu_event:
			break;
		case R.id.menu_update_mark:
			updateMark();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void updateMark() {
		SendMultiMark mark = new SendMultiMark(this);
		mark.execute();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			GetListCourseTask task = new GetListCourseTask(this);
			task.setOnGetListCourseListener(this);
			task.execute();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		String courseId = "" + mAdapterCourse.getItem(position).getId();
		courseName = mAdapterCourse.getItem(position).getName();
		Editor editor = prefCourse.edit();
		editor.putString(COURSE_NAME_KEY, courseName);
		editor.commit();
		GetListWeekTask getListWeek = new GetListWeekTask(this, courseId);
		getListWeek.setOnGetListWeekListener(this);
		getListWeek.execute();
	}

	@Override
	public void onGetListCourseListener(ArrayList<CourseItem> data) {
		if (data.size() == 0) {
			txtGreeting.setVisibility(View.VISIBLE);
			mListCourse.setVisibility(View.GONE);
		} else {
			mListCourse.setVisibility(View.VISIBLE);
			txtGreeting.setVisibility(View.GONE);
			mAdapterCourse.clear();
			for (CourseItem item : data)
				mAdapterCourse.add(item);
			mAdapterCourse.notifyDataSetChanged();
		}
	}

	@Override
	public void onGetListWeekListener(ArrayList<WeekItem> data) {
		Intent intent = new Intent(getApplicationContext(), TopicaWeek.class);
		String startDate;
		String endDate;
		int index = 0;
		for (int i = 0; i < data.size(); i++) {
			try {
				startDate = data.get(i).getStartWeek() + " 00:00";
				endDate = data.get(i).getEndWeek() + " 23:59";
				boolean isThisWeek = Util.isCurrentTimeBetween(startDate,
						endDate);
				index++;
				if (isThisWeek) {
					break;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		intent.putExtra(TopicaWeek.LIST_WEEK, data);
		if (index == data.size()) {
			index = 0;
		}
		System.out.println("index= " + index);
		intent.putExtra(TopicaWeek.INDEX, index);
		startActivity(intent);
	}

	@Override
	public void onSendMultiMarkFiinish() {

	}

}
